package com.yand.controller.system;


import com.github.pagehelper.PageInfo;
import com.yand.api.entity.common.Result;
import com.yand.api.entity.system.TSysMenu;
import com.yand.api.entity.system.TSysRole;
import com.yand.api.service.system.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;


/**
 * 角色相关的控制器
 *
 */
@Api(tags = "角色相关接口逻辑")
@RestController
@RequestMapping("system")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 跳转角色管理列表页面
     * @return
     */
    @ApiOperation("跳转角色列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "role", value = "角色", required = true)
    })
    @GetMapping("/role")
    public ModelAndView toRolePage(TSysRole role, Model model){

        ModelAndView modelAndView = new  ModelAndView();

        modelAndView.setViewName("system/role/role-list");

        PageInfo<TSysRole> rolePageInfo = roleService.queryRoleList(role);

        model.addAttribute("pageInfo",rolePageInfo);

        return modelAndView;
    }

    @ApiOperation("跳转角色添加/编辑页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true)
    })
    @GetMapping("/role/{roleId}")
    public ModelAndView toEditRolePage(@PathVariable(value = "roleId") Integer roleId, Model model){

        ModelAndView modelAndView = new  ModelAndView();

        String typeSuffix = (roleId == 0) ? "add":"edit";

        modelAndView.setViewName("system/role/role-"+typeSuffix);

        TSysRole role = roleService.getRole(roleId);

        model.addAttribute("role",role);

        return modelAndView;
    }



    /**
     * 添加角色方法
     * @param role
     * @return
     */
    @ApiOperation("添加角色后台方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "role", value = "角色", required = true)
    })
    @PostMapping("/role")
    public Result addRole(TSysRole role){

        roleService.addRole(role);

        return new Result(0);
    }

    @ApiOperation("修改角色后台方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "role", value = "角色", required = true)
    })
    @PutMapping("/role")
    public Result editRole(TSysRole role){

        roleService.editRole(role);

        return new Result(0);
    }


    @ApiOperation("删除角色后台方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true)
    })
    @DeleteMapping("/role/{roleId}")
    public Result delRole(@PathVariable(value = "roleId") Integer roleId){

        roleService.delRole(roleId);

        return new Result(0);
    }




    @ApiOperation("通过角色ID获取菜单树的方法")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleId", value = "角色ID", required = true)
    })
    @PostMapping("/role/menu/{roleId}")
    public List<TSysMenu> getMenuTree(@PathVariable(value = "roleId") Integer roleId){

        List<TSysMenu> menuTree = roleService.getMenuTreeListByRoleId(roleId);

        return menuTree;
    }






}
